#include <QModelIndexList>
#include <QMouseEvent>
#include <QItemSelectionModel>

#include "ScheduleTableView.h"

ScheduleTableView::ScheduleTableView(QWidget* parent):QTableView(parent)
{
	//setSelectionMode(QAbstractItemView::ExtendedSelection);

	siesta = false;
	night = false;
	for(int i = 0; i < 64;++i)
	{
		rowDivisions.append(fifteen);
	}
	
	
	installEventFilter(this);
	this->nextWeek = NULL;
	this->prevWeek = NULL;
	this->menu = NULL;
	this->div = fifteen;
	connect(this,SIGNAL(doubleClicked(const QModelIndex&)),this,SLOT(handleDoubleClick(const QModelIndex&)));
	//bool b = connect(this,SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),this,SLOT(handleSelection(const QItemSelection&,const QItemSelection&)));
	connect(this,SIGNAL(pressed(const QModelIndex&)),this,SLOT(handleMousePress(const QModelIndex&)));
	this->setEditTriggers(QAbstractItemView::AnyKeyPressed);
}

QList<Schedule*> ScheduleTableView::getSelectedSchedules()
{
	QPersistentModelIndex first = tableModel->getSelectedCells().first;
	QPersistentModelIndex last = tableModel->getSelectedCells().second;
	if(!first.isValid() || !last.isValid())
		return QList<Schedule*>();
	QDateTime* f = tableModel->getModel()->getCurrentTimeTable()[first.row()][first.column()];
	QDateTime* l = tableModel->getModel()->getCurrentTimeTable()[last.row()+1][first.column()];
	QPair<QDateTime*,QDateTime*>* pair = new QPair<QDateTime*,QDateTime*>(f,l);
	QList<Schedule*> list = tableModel->getModel()->getSchedules(pair);
	delete pair;
	return list;
}

Schedule* ScheduleTableView::getSelectedSchedule()
{
	QPersistentModelIndex first = tableModel->getSelectedCells().first;
	QPersistentModelIndex last = tableModel->getSelectedCells().second;
	if(!first.isValid() || !last.isValid())
		return 0;
	if(first.column() != last.column())
		return 0;
	if(tableModel->getModel()->getCurrentTimeTable()[first.row()][first.column()]->date() < QDate::currentDate())
		return 0;
	
	QDateTime* f = tableModel->getModel()->getCurrentTimeTable()[first.row()][first.column()];
	QDateTime* l = tableModel->getModel()->getCurrentTimeTable()[last.row()+1][first.column()];
	Schedule* s = tableModel->getModel()->getSchedule(f);
	return s;
}

void ScheduleTableView::doDelete(Schedule* s)
{

}

void ScheduleTableView::deleteSchedules()
{
	QList<Schedule*> schedules = this->getSelectedSchedules();
	if(schedules.isEmpty())
		return;

	QMessageBox box(QMessageBox::Question,"Deleting schedule",QString("Do you really want to delete %1 schedule(s)?").arg(schedules.count()),QMessageBox::Ok | QMessageBox::Cancel);
	int ret = box.exec();
	if(ret == QMessageBox::Ok)
	{
		this->tableModel->getModel()->deleteSchedules(schedules);
		this->unMerge();
	}
}

void ScheduleTableView::keyPressEvent(QKeyEvent* event)
{

	QPersistentModelIndex first = tableModel->getSelectedCells().first;
	QPersistentModelIndex last = tableModel->getSelectedCells().second;
	if(!first.isValid() || !last.isValid())
		return;
	if(first.column() != last.column())
		return;
	if(tableModel->getModel()->getCurrentTimeTable()[first.row()][first.column()]->date() < QDate::currentDate())
		return;
	
	QDateTime* f = tableModel->getModel()->getCurrentTimeTable()[first.row()][first.column()];
	QDateTime* l = tableModel->getModel()->getCurrentTimeTable()[last.row()+1][first.column()];
	Schedule* s = tableModel->getModel()->getSchedule(f);	
	if(event->key() >= 0x20 && event->key() <= 0x0FF || event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
	{
		
		if(s && *s->getEndDate() ==*l)
		{
			this->tableModel->setEditing(true);
			this->edit(this->tableModel->index(first.row(),first.column()));
		}
		else
		{
			for(int i = first.row(); i <= last.row();++i)
			{
				QDateTime* t = tableModel->getModel()->getCurrentTimeTable()[i][first.column()];
				if(tableModel->getModel()->getSchedule(t))
					return;
			}
			this->merge();
			if(event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return && ! this->tableModel->isEditing())
			{
				tableModel->addSchedule("");
			}
			else
			{
				this->tableModel->setEditing(true);
				QTableView::keyPressEvent(event);
			}
			return;
		}
	}

	if(event->key() == Qt::Key_Delete)
	{
		deleteSchedules();
	}
}


void ScheduleTableView::editorClosed(QWidget* editor, QAbstractItemDelegate::EndEditHint hint)
{
	if(tableModel->isEditing())
	{
		this->unMerge();
		tableModel->setEditing(false);
	}
}

void ScheduleTableView::handleMousePress(const QModelIndex& index)
{
	tableModel->setEditing(false);
	tableModel->refreshTable();
	//TODO
}
/*
void ScheduleTableView::mouseReleaseEvent(QMouseEvent* event)
{
	
	if(event->button() == Qt::LeftButton && this->indexAt(event->pos()).isValid())
	{
		QModelIndex index = this->indexAt(event->pos());
		// TODO
	}
	
}
*/
void ScheduleTableView::leaveEvent(QEvent * event)
{
	// hide hovered row
	tableModel->setHoveredRow(-1);
}

void ScheduleTableView::setRowSpans()
{
	QList<QPair<QPair<int,int>,int>>* expandList = tableModel->getRowsToSpan();
	for(int i = 0; i < expandList->count();++i)
	{
		setSpan(expandList->at(i).first.first, expandList->at(i).first.second, expandList->at(i).second, 1);
	}
}

void ScheduleTableView::scrollToCurrentTime()
{
	if(model() == NULL)
		return;
	QDateTime currentTime = QDateTime::currentDateTime();
	int rowIndex = (currentTime.time().hour() - 9)*4 + currentTime.time().minute() / 15;
	int columnIndex = (currentTime.date().dayOfWeek() +2) %7;
	scrollTo(model()->index(rowIndex,columnIndex-1),QAbstractItemView::PositionAtTop);
}

void ScheduleTableView::setupMenu()
{
		menu = new QMenu();
		menuActions.append(menu->addAction("Reserve"));
		menuActions.append(menu->addAction("Set back free"));
		menuActions.append(menu->addAction("Open Chart"));
		menuActions.append(menu->addAction("Edit comment"));
		connect(menuActions[0],SIGNAL(triggered()),this,SLOT(merge()));
		connect(menuActions[1],SIGNAL(triggered()),this,SLOT(deleteSchedule()));
		
}



void ScheduleTableView::unMerge()
{
	QModelIndexList selected = selectionModel()->selectedIndexes();
	QModelIndex index;
	foreach(index,selected)
	{
		setSpan(index.row(),index.column(),1,1);
	}
}

void ScheduleTableView::handleDoubleClick(const QModelIndex& index)
{
	QDateTime* now = tableModel->getModel()->getCurrentTimeTable()[index.row()][index.column()];
	
	if(tableModel->getModel()->getSchedule(now) == NULL)
	{
		if(now->date() < QDate::currentDate())
			return;
		this->edit(index);
		tableModel->setEditing(true);
	}
	else
	{
		//view chart
	}
}

void ScheduleTableView::merge()
{
	QModelIndexList selected = selectionModel()->selectedIndexes();
	QModelIndex index;
	int columns = selected.last().column() - selected.first().column()+1;
	int rows = selected.count() / columns;
	for (int i = 0; i< columns; ++i)
	{
		this->setSpan(selected[i*rows].row(),selected[i*rows].column(),rows,1);
	}
}


bool ScheduleTableView::eventFilter(QObject* obj, QEvent* event)
{
	if(event->type() == QEvent::ContextMenu)
	{
		QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
		if(menu == NULL)
			setupMenu();
		QModelIndexList selected = selectionModel()->selectedIndexes();
		QModelIndex index;
		int first = selected[0].column();
		bool b = false;
		foreach(index,selected)
		{
			b = (first != index.column());
			if(b)
			{
				menuActions[0]->setDisabled(true);
				menuActions[0]->setDisabled(true);
				break;
			}
		}

		Schedule * s = getSelectedSchedule();
		if(!b)
		{
			for(int i = 0; i < menuActions.count()-2;++i)
				menuActions[i]->setDisabled(false);
		}

		
		menu->exec(QCursor::pos());

		return false;
	}
	else
		return QTableView::eventFilter(obj,event);
}

ScheduleTableView::~ScheduleTableView(void)
{
}

void ScheduleTableView::setColumnWidth(int width)
{
	for( int i = 0; i < 6; ++i)
	{
		QTableView::setColumnWidth(i,width);
	}
}

void ScheduleTableView::setRowHeight(int height)
{
	for(int i = 0; i < 64; ++i)
	{
		QTableView::setRowHeight(i,height);
	}
}

void ScheduleTableView::setStartMorningIndex(int index)
{
	this->beginMorningIndex = index;
}

void ScheduleTableView::setEndMorningIndex(int index)
{
	this->endMorningIndex = index;
}

void ScheduleTableView::setEndOfNightIndex(int index)
{
	this->endNightIndex = index;
}

void ScheduleTableView::setStartAfterNoonIndex(int index)
{
	this->beginAfterNoonIndex = index;
}

void ScheduleTableView::setEndAfterNoonIndex(int index)
{
	this->endAfterNoonIndex = index;
}

void ScheduleTableView::hideSiesta()
{
	for(int i = this->endMorningIndex ;i < beginAfterNoonIndex ; ++i)
	{
		this->setRowHidden(i,true);
	}
	
}
void ScheduleTableView::hideNight()
{
	for(int i =this->endAfterNoonIndex; i< this->endNightIndex; ++i)
	{
		this->setRowHidden(i,true);
	}

}

void ScheduleTableView::setModel(ScheduleTableModel* m )
{
	this->tableModel = m;
	QAbstractItemView::setModel(m);
}

void ScheduleTableView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
{
	QModelIndexList lst = this->selectionModel()->selectedIndexes();
	if(lst.count() == 0)
		return;
	tableModel->setSelectedCells(QPair<QPersistentModelIndex,QPersistentModelIndex>(QPersistentModelIndex(lst.first()),QPersistentModelIndex(lst.last())));
	/*
	foreach(index,lst)
	{
		m->setSelectedCells(QPair<int,int>(index.row(),index.column()));
	}
	*/
}

/*
QTableWidgetItem* ScheduleTableView::getLastHovered()
{
	return this->lastHovered;
}

void ScheduleTable::setLastHovered(QTableWidgetItem* item)
{
	this->lastHovered = item;
}

void ScheduleTable::setModel(korlapAdmin_model* model)
{
	this->model = model;
}

void ScheduleTable::initialize()
{
	this->setRowCount(34);
	this->model = model;
	this->locale = locale;
	this->resize(850,535);
	
	this->setMouseTracking(true);

	QStringList weekdays;
	this->weekRange = model->calculateDaysOfThisWeek();

	int k = 0;
	for(int i = 6; i<8;i ++)
	{
		weekdays << locale->dayName(i) + " (" + weekRange->first->date().addDays(k++).toString("dd. MMM") + ")";
	}

	for( int i = 1; i < 5;++i)
	{
		weekdays << locale->dayName(i)  + " (" + weekRange->second->date().addDays(k++).toString("dd. MMM") + ")";;
	}
	for(int i = 0; i < 6; ++i)
	{
		this->setColumnWidth(i,130);
	}
	
	QTime beginTime(beginningOfMorningShift,0);
	QTime endTime(this->endOfMorningShift,0);
	//QTime endTime(23,45);
	QStringList timeList;
	while(beginTime <= endTime)
	{
		timeList << beginTime.toString("hh:mm ");
		beginTime = beginTime.addSecs(900);
	}
	
	QTime beginNightShift(beginningOfNightShift,0);
	QTime endNightShift(endOfNightShift,0);
	while(beginNightShift <= endNightShift)
	{
		timeList << beginNightShift.toString("hh:mm ");
		beginNightShift = beginNightShift.addSecs(900);
	}
	this->setHorizontalHeaderLabels(weekdays);
	this->setVerticalHeaderLabels(timeList);
	brushTable();
	bool ok =connect(this,SIGNAL(cellEntered(int, int)),this,SLOT(setLabel(int, int)));
	
	if(QTime::currentTime() < QTime(this->beginningOfMorningShift,0))
	{
		this->currentShift = Shift::MORNING;
	}
	else if(QTime::currentTime() < QTime(this->endOfMorningShift,0))
	{
		this->currentShift = Shift::MORNING;
	}else if(QTime::currentTime() < QTime(this->beginningOfNightShift,0))
	{
		this->currentShift = Shift::SIESTA;
		this->doShowSiesta(2);
	}else if(QTime::currentTime() < QTime(this->endOfNightShift,0))
	{
		this->currentShift = Shift::AFTERNOON;
	}else if(QTime::currentTime() >= QTime(this->endOfNightShift,0))
	{
		this->currentShift = Shift::NIGHT;
		this->doShowNight(2);
	}

	jumpToCurrentTime();
}

void ScheduleTable::jumpToCurrentTime()
{
	QTime time = QTime::currentTime();
	int hour = time.hour();
	int min = time.minute();
	//if(currentShift
}

QTableWidgetItem* ScheduleTable::morningItem()
{
	return item(0,0);
}

QTableWidgetItem* ScheduleTable::siestaItem()
{
	return item((this->endOfMorningShift-this->beginningOfMorningShift)*4,0);
}
void ScheduleTable::jumpToMorning()
{
	scrollToItem(morningItem(),QAbstractItemView::PositionAtTop);
}

void ScheduleTable::jumpToAfternoon()
{
	scrollToItem(afternoonItem(),QAbstractItemView::PositionAtTop);
}

void ScheduleTable::jumpToSiesta()
{
	scrollToItem(siestaItem(),QAbstractItemView::PositionAtTop);
}

void ScheduleTable::jumpToNight()
{
	scrollToItem(nightItem(),QAbstractItemView::PositionAtTop);
}
QTableWidgetItem* ScheduleTable::afternoonItem()
{
	int row = (this->endOfMorningShift - this->beginningOfMorningShift)*4 +1;
	if(!siesta)
		return item(row,0);
	else
		return item(row + siestaTime*4,0);
}

QTableWidgetItem* ScheduleTable::nightItem()
{
	int row = (this->endOfNightShift- this->beginningOfMorningShift)*4 +1;
	if(siesta)
	{
		return item(row,0);	
	}
	else
	{
		row -= this->siestaTime*4;
		return item(row,0);
	}
}
*/

void ScheduleTableView::jumpToMorning()
{
	scrollTo(model()->index(0,0),QAbstractItemView::PositionAtTop);
}

void ScheduleTableView::jumpToAfternoon()
{
	scrollTo(model()->index(this->beginAfterNoonIndex,0),QAbstractItemView::PositionAtTop);
}

void ScheduleTableView::setThirtyMinDiv(bool checked)
{
	if(!checked)return;
	for(int i = 0; i < this->endMorningIndex; i = i++)
	{
		i % 2 == 0 ? setRowHidden(i,false) : setRowHidden(i,true);
	}
	if(siesta)
	{
		for(int i = this->endMorningIndex; i < this->beginAfterNoonIndex;++i)
		{
			i % 2 == 0 ? setRowHidden(i,false): setRowHidden(i,true);
		}
	}
	for(int i = this->beginAfterNoonIndex; i < this->endAfterNoonIndex; ++i)
	{
		i % 2 == 0 ? setRowHidden(i,false): setRowHidden(i,true);
	}

	if(night)
	{
		for(int i = this->endAfterNoonIndex; i < this->endNightIndex; ++i)
		{
			i % 2 == 0 ? setRowHidden(i,false): setRowHidden(i,true);
		}
	}
	this->div = thirty;
}

void ScheduleTableView::setOneHourDiv(bool checked)
{
	if(!checked)return;
	
	for(int i = 0; i < this->endMorningIndex; i = i++)
	{
		i % 4 == 0 ? setRowHidden(i,false) : setRowHidden(i,true);
	}
	if(siesta)
	{
		for(int i = this->endMorningIndex; i < this->beginAfterNoonIndex;++i)
		{
			i % 4 == 0 ? setRowHidden(i,false): setRowHidden(i,true);
		}
	}
	for(int i = this->beginAfterNoonIndex; i < this->endAfterNoonIndex; ++i)
	{
		i % 4 == 0 ? setRowHidden(i,false): setRowHidden(i,true);
	}

	if(night)
	{
		for(int i = this->endAfterNoonIndex; i < this->endNightIndex; ++i)
		{
			i % 4 == 0 ? setRowHidden(i,false): setRowHidden(i,true);
		}
	}
	this->div = hour;
}

void ScheduleTableView::setTwoHoursDiv(bool checked)
{
	if(!checked)return;
	
	for(int i = 0; i < this->endMorningIndex; i = i++)
	{
		i % 8 == 0 ? setRowHidden(i,false) : setRowHidden(i,true);
	}
	if(siesta)
	{
		for(int i = this->endMorningIndex; i < this->beginAfterNoonIndex;++i)
		{
			i % 8 == 0 ? setRowHidden(i,false): setRowHidden(i,true);
		}
	}
	for(int i = this->beginAfterNoonIndex; i < this->endAfterNoonIndex; ++i)
	{
		i % 8 == 0 ? setRowHidden(i,false): setRowHidden(i,true);
	}

	if(night)
	{
		for(int i = this->endAfterNoonIndex; i < this->endNightIndex; ++i)
		{
			i % 8 == 0 ? setRowHidden(i,false): setRowHidden(i,true);
		}
	}
	this->div = twohours;
}

void ScheduleTableView::setFifteenMinDiv(bool checked)
{
	if(!checked)return;
	
	for(int i = 0; i < this->endMorningIndex; i = i++)
	{
		setRowHidden(i,false);
	}
	if(siesta)
	{
		for(int i = this->endMorningIndex; i < this->beginAfterNoonIndex;++i)
		{
			setRowHidden(i,false);
		}
	}
	for(int i = this->beginAfterNoonIndex; i < this->endAfterNoonIndex; ++i)
	{
		setRowHidden(i,false);
	}

	if(night)
	{
		for(int i = this->endAfterNoonIndex; i < this->endNightIndex; ++i)
		{
			setRowHidden(i,false);
		}
	}
	this->div = fifteen;
}

void ScheduleTableView::doShowNight(int state)
{
	

	if(state == 2)
	{
		showNight();
		this->night = true;
	}
	else
	{
		hideNight();
		scrollTo(model()->index(this->endMorningIndex,0),QAbstractItemView::PositionAtTop);
		this->night = false;
	}	
	
}

void ScheduleTableView::doShowSiesta(int state)
{
	if(state == 2)
	{
		showSiesta();
		this->siesta= true;
	}
	else
	{
		hideSiesta();
		scrollTo(model()->index(this->beginAfterNoonIndex,0),QAbstractItemView::PositionAtTop);
		this->siesta = false;
	}
	
}


void ScheduleTableView::showSiesta()
{
	switch(div)
	{
		case fifteen:
			for(int i = this->endMorningIndex; i < this->beginAfterNoonIndex;i++)
			{
				setRowHidden(i,false);
			}
			break;
		case thirty:
			for(int i = this->endMorningIndex;i < this->beginAfterNoonIndex;i = i+2)
			{
				setRowHidden(i,false);
			}
			break;
		case hour:
			for(int i = this->endMorningIndex; i < this->beginAfterNoonIndex; i = i + 4)
			{
				setRowHidden(i,false);
			}
			break;
		case twohours:
			for(int i = this->endMorningIndex; i < this->beginAfterNoonIndex; i = i + 8)
			{
				setRowHidden(i,false);
			}
			break;
	}
	
	scrollTo(this->model()->index(endMorningIndex-1,0),QAbstractItemView::PositionAtTop);
}

void ScheduleTableView::showNight()
{
	switch(div)
	{
		case fifteen:
			for(int i = this->endAfterNoonIndex; i < this->endNightIndex;i++)
			{
				setRowHidden(i,false);
			}
			break;
		case thirty:
			for(int i = this->endAfterNoonIndex;i < this->endNightIndex;i = i+2)
			{
				setRowHidden(i,false);
			}
			break;
		case hour:
			for(int i = this->endAfterNoonIndex; i < this->endNightIndex; i = i + 4)
			{
				setRowHidden(i,false);
			}
			break;
		case twohours:
			for(int i = this->endAfterNoonIndex; i < this->endNightIndex; i = i + 8)
			{
				setRowHidden(i,false);
			}
			break;
	}

	scrollTo(model()->index(endAfterNoonIndex,0),QAbstractItemView::PositionAtTop);
}
/*
void ScheduleTable::brushFraction(int begin, int end)
{
	QColor color(0,255,0,60);
	QColor colorEnd(0,200,0,150);

	QLinearGradient gradient(0,0,130,10);
	gradient.setColorAt(0,color);
	gradient.setColorAt(1,colorEnd);
	QBrush brush(gradient);

	for(int i = begin; i < end; ++i)
	{
		for(int j = 0; j < this->columnCount(); ++j)
		{
			QTableWidgetItem * item = new QTableWidgetItem();
			item->setBackground(brush);
			setItem(i,j,item);
		}
	}
}

void ScheduleTable::setMorningBegin(int i)
{
	this->beginningOfMorningShift = i;
}

void ScheduleTable::setMorningEnd(int i)
{
	this->endOfMorningShift = i;
}

void ScheduleTable::setNightBegin(int i)
{
	this->beginningOfNightShift = i;
}

void ScheduleTable::setNightEnd(int i)
{
	this->endOfNightShift = i;
}

void ScheduleTable::setSiestaTime(int i)
{
	this->siestaTime = i;
}

void ScheduleTable::brushTable()
{
	try
	{
		QList<Schedule *> schedules = model->getSchedulesForThisWeek();
	}
	catch(Exception e)
	{
		throw e;
	}

	int rows = this->rowCount();
	int cols = this->columnCount();
	QColor color(0,255,0,60);
	QColor colorEnd(0,200,0,150);

	QLinearGradient gradient(0,0,130,10);
	gradient.setColorAt(0,color);
	gradient.setColorAt(1,colorEnd);
	QBrush brush(gradient);

	for(int i = 0; i < rows; ++i)
	{
		for(int j = 0; j < cols; ++j)
		{
			QTableWidgetItem * item = new QTableWidgetItem();
			item->setBackground(brush);
			setItem(i,j,item);
			
		}
	}
}

QPair<QDateTime*,QDateTime*>* ScheduleTable::getWeekRange()
{
	return this->weekRange;
}

void ScheduleTable::showSiesta(bool show)
{
	this->siesta = show;
}

void ScheduleTable::showNight(bool show)
{
	this->night = show;
}


void ScheduleTable::brushGreen(QTableWidgetItem* item)
{
	
	QColor color(0,255,0,60);
	QColor colorEnd(0,200,0,150);

	QLinearGradient gradient(0,0,130,10);
	gradient.setColorAt(0,color);
	gradient.setColorAt(1,colorEnd);
	QBrush brush(gradient);
	item->setBackground(brush);
}

void ScheduleTable::brushRed(QTableWidgetItem* item)
{
	QColor color2(255,0,0,100);
	QColor color2End(150,0,0);
	QLinearGradient gradientReserved(0,0,300,10);
	gradientReserved.setColorAt(0,color2);
	gradientReserved.setColorAt(1,color2End);
	QBrush redBrush(gradientReserved);
	item->setBackground(redBrush);
}

void ScheduleTableView::hoverCell(const QModelIndex& index)
{
	QVariant data = model()->headerData(index.row(),Qt::Vertical);
	QString hour = data.toString();
	data = model()->headerData(index.column(),Qt::Vertical);
	QString day = data.toString();
	QString dayname = day.split(" ")[0];
	this->currentHoveredLabel = hour + ", " + dayname;
	
	if(this->lastHovered != NULL)
	{
		if(lastHovered->checkState() == Qt::Unchecked)
		{
			brushGreen(lastHovered);
		}
		else
		{
			brushRed(lastHovered);
		}
		lastHovered->setText("");
	}
	lastHovered = item;
	//this->timeSelector->setLastHovered(item);
	if(item->checkState() ==Qt::Unchecked)
	{
		QColor color(255,255,130,100);
		QColor colorEnd(255,255,50,150);

		QLinearGradient gradient(0,0,300,10);
		gradient.setColorAt(0,color);
		gradient.setColorAt(1,colorEnd);
		QBrush brush(gradient);
		item->setBackground(brush);
		item->setText(currentHoveredLabel);
	}
	else
	{
		QColor color2(255,0,0,50);
		QColor color2End(150,0,50);
		QLinearGradient gradientReserved(0,0,300,10);
		gradientReserved.setColorAt(0,color2);
		gradientReserved.setColorAt(1,color2End);
		QBrush redBrush(gradientReserved);
		item->setBackground(redBrush);
	}

}
*/
/*
void ScheduleTable::brushItem(const QModelIndex& index)
{
	 //this->timeSelector->getLastHovered();
	if(this->lastHovered != NULL)
	{
		if(lastHovered->checkState() == Qt::Unchecked)
		{
			brushGreen(lastHovered);
		}
		else
		{
			brushRed(lastHovered);
		}
		lastHovered->setText("");
	}
	lastHovered = item;
	//this->timeSelector->setLastHovered(item);
	if(item->checkState() ==Qt::Unchecked)
	{
		QColor color(255,255,130,100);
		QColor colorEnd(255,255,50,150);

		QLinearGradient gradient(0,0,300,10);
		gradient.setColorAt(0,color);
		gradient.setColorAt(1,colorEnd);
		QBrush brush(gradient);
		item->setBackground(brush);
		item->setText(currentHoveredLabel);
	}
	else
	{
		QColor color2(255,0,0,50);
		QColor color2End(150,0,50);
		QLinearGradient gradientReserved(0,0,300,10);
		gradientReserved.setColorAt(0,color2);
		gradientReserved.setColorAt(1,color2End);
		QBrush redBrush(gradientReserved);
		item->setBackground(redBrush);
	}
}
*/